package com.test.stock.controller;

import com.test.stock.service.StockService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * 库存服务
 *
 * @author IT贱男
 * @version v1.0.0
 * @date 2022/11/7 16:38
 */
@RestController
@RequestMapping("/stock")
@AllArgsConstructor
public class StockController {

    private final StockService stockService;

    @GetMapping("/getStockByProductId")
    public Integer getStockByProductId(Integer productId) {
        return stockService.selectStockByProductId(productId);
    }


    public static void main(String[] args) {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setDaemon(true);
                thread.setName("com.alibaba.nacos.naming.beat.sender");
                return thread;
            }
        });
        scheduledThreadPoolExecutor.schedule(new Task(), 5000, TimeUnit.MILLISECONDS);
    }
}

class Task implements Runnable {

    ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setName("com.alibaba.nacos.naming.beat.sender");
            return thread;
        }
    });

    @Override
    public void run() {
        System.out.println(new Date());

        scheduledThreadPoolExecutor.schedule(new Task(), 5000, TimeUnit.MILLISECONDS);
    }
}